package dfl.bysj.controller;


import dfl.bysj.entity.HonestStudent;
import dfl.bysj.result.R;
import dfl.bysj.service.HonestGradeService;
import dfl.bysj.service.HonestStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DFL
 * @since 2022-04-26
 */
@Slf4j
@RestController
@RequestMapping("/honestStudent")
public class HonestStudentController {

    private HonestStudentService honestStudentService;

    private HonestGradeService honestGradeService;

    @Autowired
    public void setHonestGradeService(HonestGradeService honestGradeService) {
        this.honestGradeService = honestGradeService;
    }

    @Autowired
    public void setHonestStudentService(HonestStudentService honestStudentService) {
        this.honestStudentService = honestStudentService;
    }

    @GetMapping("/id")
    public R id(@RequestHeader("Authorization")String token){
        //1、检查学生信用情况
        honestStudentService.getHonestByToken(token);
        return R.ok();
    }

    @GetMapping("/grade")
    public R grade(String stuId){
        return R.ok().data("grade",honestGradeService.getHonestGradeByStuId(Integer.parseInt(stuId)));
    }

    @GetMapping("/hon")
    public R hon(Integer hon_id,Integer stu_use_id){
       List<HonestStudent> honestStudents= honestStudentService.getHonestsByHonId(hon_id,stu_use_id);
        Integer count=honestStudents.size();
        LocalDateTime localDateTime= honestStudents.get(0).getCreateTime();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String dateStr = formatter.format(localDateTime);
        Map<String,Object> map=new HashMap<>();
        map.put("count",count);
        map.put("time", dateStr);
        return R.ok().data(map);
    }

    @GetMapping("/grade/stuUseId")
    public R stuUseId(String stuUseId){
        return R.ok().data("grade",honestGradeService.getHonestGradeByStuUseId(Integer.valueOf(stuUseId)));
    }

    @GetMapping("/stuId")
    public R stuId(String stuId){
        return R.ok().data("grade",honestGradeService.getHonestGradeByStuId(Integer.valueOf(stuId)));
    }
}

